package sen.queryUtils;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 新的查询辅助类
 * 自动生成条件语句再查询返回结果
 * @author Administrator
 */
public class HBQueryBean3 {

	private Session session;
	private List<Cond> conds;
	
	
	/**
	 * (sql/countSql)与bean必须至少设置一个
	 */
	
	/**指定查询sql和统计countSql--start**/
	private String sql;
	private String countSql;
	/**指定查询sql和统计countSql--end**/
	
	/**
	 * 指定查询哪个bean
	 */
	private Class<?> bean;
	private String tableName;
	
	/**
	 * 用指定的bean进行hql查询
	 */
	public HBQueryBean3(Session session, List<Cond> conds, Class<?> bean) {
		super();
		this.session = session;
		this.conds = conds;
		this.bean = bean;
	}
	/**
	 * 用指定的sql与countSql进行查询，具体是本地sql查询还是hql查询，得看具体调用的方法
	 * @param session
	 * @param conds
	 * @param sql
	 * @param countSql
	 */
	public HBQueryBean3(Session session, List<Cond> conds, String sql,
			String countSql) {
		super();
		this.session = session;
		this.conds = conds;
		this.sql = sql;
		this.countSql = countSql;
	}
	/**
	 * 用指定table名进行本地sql查询
	 * @param session
	 * @param conds
	 * @param tableName
	 */
	public HBQueryBean3(Session session, List<Cond> conds,
			String tableName) {
		super();
		this.session = session;
		this.conds = conds;
		this.tableName = tableName;
	}
	/**
	 * 内部查询方法
	 * @param isHql:
	 * 	true	:用hql查询
	 *  false	:用native查询
	 * @return
	 */
	private Results query(boolean isHql) {
		
		if(MyUtils.isNull(sql)) {
			if(isHql) {
				if(MyUtils.isNull(bean)){
					throw new RuntimeException("如果没指定hql并且您调用的是queryHQL()的话，则必须指定bean。");
				}
				sql = "from " + bean.getName();
			} else {
				if(MyUtils.isNull(tableName)){
					throw new RuntimeException("如果没指定sql并且您调用的是queryNative()的话，则必须指定tableName。");
				}
				sql = "select * from " + tableName;
			}
		}
		W wo = W.getW(conds);
		boolean ifPaging = wo.ifPaging();
		
		if(MyUtils.isNull(countSql) && ifPaging){
			if(isHql){
				countSql = " select count(*) from "+ bean.getName();
			} else {
				countSql = "select count(*) from "+ tableName;
			}
		}
		
		//这句得放在getWhereStr()之后，因为col,order的值在getWhereStr之后才被设置
		String orderStr = wo.getOrderStr();
		
		sql += wo.getWhereStr() + orderStr;
		if(ifPaging){
			countSql += wo.getWhereStr();
		}
		
		Query query = null;
		if(isHql) {
			query = session.createQuery(sql);
		} else {
			query = session.createSQLQuery(sql);
		}
		if(ifPaging) {
			query.setFirstResult(wo.getRowStart());
			query.setMaxResults(wo.getPageSize());
		}
		
		Results results = new Results(query.list());
		if(ifPaging) {
			//查总记录数
			long total = 0;
			Query qCount = null;
			Object totalObj = null;
			if(isHql) {
				qCount = session.createQuery(countSql);
				totalObj = qCount.uniqueResult();
				if (totalObj!=null) {
					total = (Long)totalObj;
				}
			} else {
				qCount = session.createSQLQuery(countSql);
				
				totalObj = qCount.uniqueResult();
				if(totalObj!=null){
					try {
						total = Long.parseLong(totalObj + "");
					} catch (NumberFormatException e) {
					}
				}
			}
			results.setTotal(total);
			results.setRowStart(wo.getRowStart());
		}
		if(results.getTotal()==0) {
			results.setTotal(results.getDatas()!=null?results.getDatas().size():0);
		}
		return results;
	}
	
	/**
	 * 使用HQL查询
	 * @return
	 */
	public Results queryHQL() {
		return query(true);
	}
	/**
	 * 使用本地查询
	 * @return
	 */
	public Results queryNative() {
		return query(false);
	}
}